//
//  CMDBModifyEmailViewController.swift
//  CMDB
//
//  Created by 王磊 on 18/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBModifyEmailViewController: NormalTitleViewController {
    
    private var email : CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    override func setup() {
        super.setup()
        
        buildSubview()
    }
    private var confirmBtn: UIButton = UIButton(type: .Custom)
    
    private func buildSubview() {
        
        contentView?.backgroundColor = UIColor.whiteColor()
        
        contentView!.addSubview(email)
        
        contentView!.addSubview(confirmBtn)
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        email.attrText = NSMutableAttributedString(string: "邮箱名:", attributes: textAttr)
        
        email.placeHolderAttr = NSAttributedString(string: "请输入新的邮箱名", attributes: placeHolderAttr)
        
        email.keyboardType = .Default
        
        email.returnKeyType = .Done
        
        email.maxLength = 25
        
        email.addTarget(self, action: #selector(returnKey), controlEvent: .ReturnKey)
        
        let Left_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
        
        email.frame = CGRect(x: Left_Margin, y: 20 , width:630 * CMDB_Rate , height: 60)
        
        confirmBtn.setTitle("确定", forState: .Normal)
        
        confirmBtn.setTitle("确定", forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        confirmBtn.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        confirmBtn.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        confirmBtn.titleLabel!.font = CMDB_17_FONT
        
        confirmBtn.addTarget(self, action: #selector(confirmBtnTriggrred), forControlEvents: .TouchUpInside)
        
        confirmBtn.layer.cornerRadius = 5
        
        confirmBtn.frame = CGRectMake(Left_Margin, email.y + 60 + 40, email.width, 88 * CMDB_Rate)
    }
    
    
}
extension CMDBModifyEmailViewController {
    
    @objc private func returnKey() {
        
        view.endEditing(true)
        
        confirmBtnTriggrred()
    }
    
    @objc private func confirmBtnTriggrred() {
        
        let bindEmail = CMDBBindEmailParam()
        
        bindEmail.email = email.text ?? ""
        
        do {
            
            try CMDBUserInfoManager.getInstance.bindEmail(bindEmail, handler: {[weak self] (isSuccess, type) in
                
                self!.navigationController?.popViewControllerAnimated(true)
                
                }, error: { (rescode) in
                    
                }, failure: {
            })
            
        } catch  CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.EmailInvalid) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("请输入正确的邮箱!")
            
        } catch {
            
            
            
        }
    }
}